<?php
namespace app\blog\controller;
use app\blog\model\BlogArticle as ArticleModel;
use app\blog\model\BlogClassify as ClassifyModel;
use app\blog\model\BlogClassifyTwo as ClassifyTwoModel;
use think\Controller;
use think\Db;

class Article extends Controller
{
    //载入写文章的主页
    public function editor()
    {
        //查询文章一级分类表
        $class_one = ClassifyModel::select();
        $this->assign('class_one', $class_one);
        //载入页面
        return $this->fetch();
    }
    //载入文章详细页面
    public function article()
    {
        $id = $_GET['id'];
        //增加浏览数
        Db::name('blog_article')->where('id','=',$id)->inc('browse')->update();
        $list = ArticleModel::where('id', '=', $id)->find();
        $this->assign('list', $list);
        return $this->fetch();
    }
    //写文章的方法
    public function insert()
    {
        $User = new ArticleModel();
        $uid = session('id');
        $category = $_POST['category'];
        $category_two = $_POST['category_two'];
        if (($_POST['classify']) == 1) {
            $classify = "原创";
        } elseif (($_POST['classify']) == 2) {
            $classify = "转载";
        } elseif (($_POST['classify']) == 3) {
            $classify = "翻译";
        }
        $title = $_POST['title'];
        $synopsis = $_POST['synopsis'];
        $content = $_POST['content'];
        $created_date = date('Y-m-d h:i:s', time());
        if (!empty($_POST['submit1'])) {
            $releases = 0;
        } elseif (!empty($_POST['submit2'])) {
            $releases = 1;
        }
        $User->save([
            'uid' => $uid,
            'category' => $category,
            'category_two' => $category_two,
            'classify' => $classify,
            'title' => $title,
            'synopsis' => $synopsis,
            'content' => $content,
            'created_date' => $created_date,
            'releases' => $releases,
        ]);
        $this->success('新增成功', '/blog','','1');
    }
    //文章点赞
    public function zan()
    {
        header('Content-type:text/json;charset=utf-8');
        $id = $_POST['id'];
        $zan = $_POST['zan'];
        $update =new ArticleModel();
        if(($zan)==1){
            $update->where('id','=',$id)->inc('support')->update();
            $cid=['cid'=>1];
            return json_encode($cid);
        }elseif (($zan)==2){
            $update->where('id','=',$id)->dec('support')->update();
            $rid=['rid'=>2];
            return json_encode($rid);
        }

    }
    //查找文章
    public function article_admin()
    {
        $uid =session('id');

        if (($_POST['classify']) == 1) {
            $classify = "原创";
        } elseif (($_POST['classify']) == 2) {
            $classify = "转载";
        }elseif (($_POST['classify']) == 3){
            $classify = "翻译";
        }elseif (($_POST['classify']) == 0){
            $classify = 0;
        }
        $category = $_POST['category'];
        $category_two = $_POST['category_two'];

        if((($classify) == "0") and (($category) == "0") and (($category_two) == "0")) { //000
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }elseif((($classify) > "0") and (($category) == "0") and (($category_two) == "0")){ //100
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
                ['classify', '=', $classify],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }elseif((($classify) == "0") and (($category) > "0") and (($category_two) == "0")){ //010
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
                ['category', '=', $category],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }elseif((($classify) == "0") and (($category) == "0") and (($category_two) > "0")){ //001
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
                ['category_two', '=', $category_two],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }elseif((($classify) > "0") and (($category) > "0") and (($category_two) == "0")){ //110
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
                ['classify', '=', $classify],
                ['category', '=', $category],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }elseif((($classify) > "0") and (($category) == "0") and (($category_two) > "0")){ //101
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
                ['classify', '=', $classify],
                ['category_two', '=', $category_two],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }elseif((($classify) == "0") and (($category) > "0") and (($category_two) > "0")){ //011
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
                ['category', '=', $category],
                ['category_two', '=', $category_two],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }elseif((($classify) > "0") and (($category) > "0") and (($category_two) > "0")) { //111
            $dog = ArticleModel::where([
                ['uid', '=', $uid],
                ['releases', '=', 1],
                ['classify', '=', $classify],
                ['category', '=', $category],
                ['category_two', '=', $category_two],
            ])->order('category_two', 'asc')->select();
            return json($dog);
        }
        //        $year = $_POST['year'];
//        $month = $_POST['month'];
//        $keywords = $_POST['keywords'];
    }
    //返回写文章时ajax请求的二级分类信息
    public function classify()
    {
        header('Content-type:text/json;charset=utf-8');
        $id=$_POST['id'];
        //$list=ClassifyTwoModel::where('cid','=',$id)->column('category_two','id');
        $list=ClassifyTwoModel::where('cid','=',$id)->select();
        return json_encode($list);

    }
}
